//luogu P3376 【模板】网络最大流
//code by hello_world_djh
#include <bits/stdc++.h>

//char buf[1 << 20], *p1, *p2;
//#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2)?EOF: *p1++)

template <typename T>
T read() {
    T x = 0, f = 1;
    char ch = getchar();
    for (; ch < '0' || ch > '9'; ch = getchar())
        if (ch == '-')
            f = ~f + 1;
    for (; ch >= '0' && ch <= '9'; ch = getchar())
        x = (x << 3) + (x << 1) + (ch ^ '0');
    return x * f;
}

typedef long long ll;
const int N = 210, M = 5010;
const ll INF = (1ll << 31) - 1;

struct Edge {
    int to, nxt;
    ll w;
} edge[M << 1];
int n, m, s, t, tot = 1, head[N], cur[N], dep[N], gap[N];
ll maxflow;

void add(int u, int v, ll w) {
    edge[++tot] = Edge{v, head[u], w};
    head[u] = tot;
    return;
}

void bfs() {
    memset(dep, -1, sizeof dep);
    memset(gap, 0, sizeof gap);
    std::queue <int> q;
    dep[t] = 0;
    gap[0]++;
    q.push(t);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = head[u]; i; i = edge[i].nxt) {
            int v = edge[i].to;
            if (~dep[v]) continue;
            dep[v] = dep[u] + 1;
            gap[dep[v]]++;
            q.push(v);
        }
    }
    return;
}

ll dfs(int u, ll flow) {
    if (u == t) {
        maxflow += flow;
        return flow;
    }
    ll used = 0;
    for (int &i = cur[u]; i; i = edge[i].nxt) {
        int v = edge[i].to;
        if (edge[i].w && dep[v] + 1 == dep[u]) {
            ll d = dfs(v, std::min(edge[i].w, flow - used));
            if (d) {
                edge[i].w -= d;
                edge[i ^ 1].w += d;
                used += d;
            }
            if (used == flow)
                return used;
        }
    }
    --gap[dep[u]];
    if (!gap[dep[u]])
        dep[s] = n + 1;
    dep[u]++;
    gap[dep[u]]++;
    return used;
}

ll ISAP() {
    bfs();
    while (dep[s] < n) {
        memcpy(cur, head, sizeof head);
        dfs(s, INF);
    }
    return maxflow;
}

int main() {
    n = read<int>();
    m = read<int>();
    s = read<int>();
    t = read<int>();
    for (int i = 1, u, v; i <= m; i++) {
        u = read<int>();
        v = read<int>();
        ll w = read<ll>();
        add(u, v, w);
        add(v, u, 0);
    }
    printf("%lld\n", ISAP());
    return 0;
}